package com.youlu.campus.web.commission;

import com.youlu.campus.entity.commission.dto.DistributorApplyDTO;
import com.youlu.campus.entity.commission.dto.SharingCommissionDetailQueryDTO;
import com.youlu.campus.service.commission.DistributorApplyService;
import com.youlu.campus.service.commission.SharingCommissionDetailService;
import com.youlu.campus.web.common.base.RequestContext;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DistributorApplyController {

    @Autowired
    private DistributorApplyService distributorApplyService;


    @Autowired
    private SharingCommissionDetailService sharingCommissionDetailService;

    /**
     * 分销员申请
     * @param dto
     * @param bindingResult
     * @return
     */
    @PostMapping("/mini-app/distributor/apply")
    public QResult apply (@RequestBody @Validated DistributorApplyDTO dto, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        dto.setCurrentUser(RequestContext.getCurUser());
        distributorApplyService.apply(dto);
        return QResult.success(null);
    }

    /**
     * 获取分销者信息
     * @return
     */
    @GetMapping("/mini-app/distributor-info")
    public QResult getDistributorInfo(){
        return QResult.success(distributorApplyService.findByUserId(RequestContext.getUserId()));
    }

    /**
     * 获取分销者入口
     * @return
     */
    @GetMapping("/mini-app/distributor/entry")
    public QResult getDistributorEntry(){
        return QResult.success(distributorApplyService.distributorEntry(RequestContext.getUserId()));
    }

    /**
     * 分销者分拥列表
     * @param dto
     * @return
     */
    @PostMapping("/mini-app/distributor/commission/detail")
    public QResult list(@RequestBody SharingCommissionDetailQueryDTO dto){
        dto.setUserId(RequestContext.getUserId());
        return QResult.success(sharingCommissionDetailService.list(dto));
    }


    /**
     * 分享好友赚佣金接口
     * @return
     */
    @GetMapping("/mini-app/distributor/commission/share")
    public QResult getDistributorSharingCommission(String activityId){
        return QResult.success(distributorApplyService.getDistributorSharingCommission(RequestContext.getUserId(),activityId));
    }

}
